package com.yun.baymax.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yun.baymax.R;
import com.yun.baymax.bean.adapter.Assistant;
import com.yun.baymax.custom.OnClickListener;
import com.yun.baymax.custom.PasswordDialog;
import com.yun.baymax.utils.ToastUtils;
import com.yun.lib.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 */

public class AssistantAdapter extends RecyclerView.Adapter<AssistantAdapter.MyHolder> {
    Context mContext;
    List<Assistant> datas = new ArrayList<>();
    private LayoutInflater inflater;
    private PasswordDialog passwordDialog;
    private String password;

    public AssistantAdapter(Context context, List<Assistant> datas) {
        this.mContext = context;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_assistant, parent, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        final Assistant data = datas.get(position);
        if (data.getIvId() != 0 && data.getIvId() != -1)
            Glide.with(mContext).load(data.getIvId()).into(holder.ivPic);
        holder.name.setText(data.getName());
        holder.ivPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.getName().equals("密码管理"))
                    checkPassword(data);
                else
                    mContext.startActivity(new Intent(mContext, data.getAct()));
            }
        });
    }

    private void checkPassword(final Assistant data) {
        password = (String) SPUtils.get(mContext, "innerPassword", "");
        passwordDialog = TextUtils.isEmpty(password) ? PasswordDialog.newInstance(0) : PasswordDialog.newInstance(1);

        passwordDialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(String keyword, int type) {
                if (type == 0) {
                    SPUtils.put(mContext, "innerPassword", keyword);
                } else if (!keyword.equals(password)) {
                    ToastUtils.showShortToast("密码错误！");
                    return;
                }
                passwordDialog.dismiss();
                mContext.startActivity(new Intent(mContext, data.getAct()));
            }
        });
        passwordDialog.setOnResetListener(new OnClickListener() {
            @Override
            public void onClick(String keyword, int type) {
                SPUtils.put(mContext, "innerPassword", "");
                ToastUtils.showShortToast("密码已清空");
            }
        });
        passwordDialog.show(data.getFragmentManager(), "");
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private ImageView ivPic;
        private TextView name;

        public MyHolder(View itemView) {
            super(itemView);
            ivPic = (ImageView) itemView.findViewById(R.id.iv_pic);
            name = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
}